package game.core.role
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import game.core.BulletFactory;
	import game.core.GameObject;
	import game.core.GameSystem;
	import game.core.bullet.Bullet;
	import game.manager.XMLManager;
	
	public class DefaultRole extends GameObject
	{
		private var bullet:Bullet;
		
		/**
		 *按键集合 
		 */		
		public var keySet:Object = {};
		/**
		 *是否按下鼠标左键 
		 */		
		public var isMouseDown:Boolean = false;
		
		public var mouseSite:Point;
		
		public function DefaultRole()
		{
			super();
			this.collidable = false;
			createRole();
		}
		
		/**
		 * 创建角色
		 */ 
		private var role:Sprite;
		private var roleHeight:Number;
		private var roleWidth:Number;
		private var roleSpeed:Number;
				
		public function createRole():void {
			role = new Sprite();
			roleSpeed = XMLManager.instance.getConfig("config",null,"roleSpeed");
			var radius:Number = 10;
			var g:Graphics = role.graphics;
			g.clear();
			g.beginFill(0xFF3344,1);
			g.drawCircle(0,0,radius);
			g.endFill();
			this.addChild(role);
			this.x = (GameSystem.instance.contentWidth - radius) / 2;
			this.y = GameSystem.instance.conentHeight - radius;
			roleHeight = role.height;
			roleWidth = role.width;
			GameSystem.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandle);
			GameSystem.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandle);
			GameSystem.instance.stage.addEventListener(MouseEvent.MOUSE_DOWN,clickHandle);
			GameSystem.instance.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandle);
		}
		
		public function activeRole():void{
			
		}
		private function keyDownHandle(e:KeyboardEvent):void{
			keySet[e.keyCode] = true;
		}
		private function keyUpHandle(e:KeyboardEvent):void{
			keySet[e.keyCode] = false;
		}
		private function clickHandle(e:MouseEvent):void{
			isMouseDown = true;
		}
		public function shoot():void {
			if(bullet){
				bullet.dispose();
				bullet = null;
			}
			bullet = BulletFactory.newInstance(this.bulletKind) as Bullet;
			bullet.x = this.x;
			bullet.y = this.y;
			bullet.shoot();// 射击之后就开始检测
			GameSystem.instance.addBullet(bullet);
		}
		
		override public function set pause(value:Boolean):void {
			super.pause = value;
			if(pause){
				this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
			}else {
				if(!this.hasEventListener(Event.ENTER_FRAME)){
					this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);// 需要判断一下,是否停止了,如果停止了,需要不能进行了
				}
			}
		}
		
		private var _bulletKind:String;
		
		public function set bulletKind(value:String):void {
			this._bulletKind = value;
		}
		
		public function get bulletKind():String {
			return this._bulletKind;
		}
		
		/**
		 *停止 
		 * 
		 */		
		public function stop():void{
			
		}
		/**
		 *跑动 1=左 2=右  
		 * @param d 方向
		 * 
		 */			
		public function run(d:int, speed:Number = 0):void{
			if(speed != 0){
				if(d == 1){
					x -= speed;
				}else if(d == 2){
					x += speed;
				}
			}else{
				if(d == 1){
					x -= roleSpeed;
				}else if(d == 2){
					x += roleSpeed;
				}
			}
			
		}
		
		/**
		 *角色死亡 
		 * 
		 */		
		public function dead():void{
			GameSystem.instance.pause();
		}
		
		private function enterFrameHandler(e:Event):void{
			if(!keySet[37] && !keySet[39] && !keySet[65] && !keySet[68]){
				stop();
			}else if((keySet[37] ||keySet[65]) && x > (roleWidth / 2 + GameSystem.instance.border)){
				run(1);
			}else if((keySet[39] || keySet[68]) && x < (GameSystem.instance.contentWidth - this.roleWidth / 2)){
				run(2);
			}
			
			if(isMouseDown ){
				shoot();
				isMouseDown = false;
			}
			if(hitTestGameObject(this)){
				dead();
			} 
			
			var minX:Number = width*0.5;
			var maxX:Number = GameSystem.instance.contentWidth - width*0.5;
			if(mouseSite == null) return;
			if((mouseSite.x) == x){
				stop();
			}else if(mouseSite.x < minX){
				mouseSite.x = minX;
			}else if(mouseSite.x > maxX){
				mouseSite.x = maxX;
			}
			if(mouseSite.x != x && mouseSite.x >= minX && mouseSite.x <= maxX){
				var speed:Number = Math.abs(mouseSite.x - x);
				if(speed < roleSpeed){
					if(mouseSite.x < x){
						run(1, speed);
					}else{
						run(2, speed);
					}
				}else{
					if(mouseSite.x < x){
						run(1)
					}else{
						run(2);
					}
				}
				
			}
		}
		/**
		 *鼠标移动事件 
		 * @param e
		 * 
		 */		
		protected function mouseMoveHandle(e:MouseEvent):void{
			mouseSite = new Point(e.stageX - GameSystem.instance.container.x, e.stageY);
		}
		
		/**
		 * 检测撞击的物品
		 * 这里的检测可能还需要有一些问题
		 * 需要优化
		 * 	1.只有2个物体的距离在一守的范围内时才去检测
		 * 	2.现在检测比较粗糙
		 */ 
		public static var count:int = 0;
		public function hitTestGameObject(value:GameObject):Boolean {
			var flag:Boolean = false;
			for each(var gameObject:GameObject in GameSystem.instance.gameObjects){
				if(gameObject.collidable && // 如果支持碰撞的话,那么继续
					gameObject !== this && // 如果是自己的话,那就不继续
					gameObject.hitTest(value)){//
					flag = true;
					count++;
					break;
				}
			}
			return flag;
		}
		
		override public function dispose():void {
			super.dispose();
			this.pause = true;
			GameSystem.instance.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandle);
			GameSystem.instance.stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandle);
			GameSystem.instance.stage.removeEventListener(MouseEvent.MOUSE_DOWN,clickHandle);
			GameSystem.instance.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandle);
		}
	}
}